#include <iostream>
#include <cmath>
#include <array>
#include <complex>
#include <cstdio>

using namespace std;

inline double delta(double a, double b, double c)
{
    return pow(b,2) - 4*a*c;
}

inline complex<double> C(const double& x)
{
    complex<double> r;
    r = x;
    return r;
}

inline string formatNumber(const complex<double>& c)
{
    double r = c.real();
    double i = c.imag();
    char *str = (char*)malloc(10);
    if (i == 0)
    {
        sprintf(str, "%f", r);
    }
    else if (i < 0)
    {
        sprintf(str, "%f - %fi", r, -i);
    }
    else
    {
        sprintf(str, "%f + %fi", r, i);
    }
    string result(str);
    free(str);
    return result;
}

array<double, 2> roots_of_real(double a, double b, double delta)
{
    array<double, 2> roots;
    roots[0] = ((-b)+sqrt(delta))/(2*a);
    roots[1] = ((-b)-sqrt(delta))/(2*a);
    return roots;
}

double root_of_zero(double a, double b)
{
    return (-b)/(2*a);
}

array<complex<double>, 2> roots_of_complex(double a, double b, double delta)
{
    array<complex<double>, 2> roots;
    roots[0] = (C(-b)+sqrt(C(delta)))/C(2*a);
    roots[1] = (C(-b)-sqrt(C(delta)))/C(2*a);
    return roots;
}

array<complex<double>, 2> roots_of(double a, double b, double c)
{
    array<complex<double>, 2> roots;
    double d = delta(a, b, c);
    if (d > 0)
    {
        array<double, 2> results = roots_of_real(a, b, d);
        roots[0] = results[0];
        roots[1] = results[1];
    }
    else if (d == 0)
    {
        roots[0] = root_of_zero(a, b);
        roots[1] = roots[0];
    }
    else
    {
        roots = roots_of_complex(a, b, d);
    }
    
    return roots;
}

int main()
{
    double a, b, c;

    cout << "请输入 3 个系数：" << endl;
    cin >> a >> b >> c;

    array<complex<double>, 2> roots = roots_of(a, b, c);

    cout << "方程的根是：" << endl;
    if (roots[0] == roots[1])
    {
        cout << formatNumber(roots[0]) << endl;
    }
    else
    {
        cout << formatNumber(roots[0]) << endl;
        cout << formatNumber(roots[1]) << endl;
    }

    return 0;
}